<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <form>
            <div>
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username" required v-model="username">
            </div>
            <div>
                <label for="password">密码:</label>
                <input type="password" id="password" name="password" required v-model="password">
            </div>
            <div>
                <button type="submit" @click="register">注册</button>
            </div>
        </form>
        <div>
            {{username }}
            {{password }}
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script>
        const { createApp } = Vue
        createApp({
            data() {
                return {
                    username: '',
                    password: ''
                }
            },
            created() {
                // this.username = 'zhangsan';
                // this.password = '123456'
            },
            methods: {
                register() {
                    if (this.username === '' || this.password === '') {
                        alert('请输入用户名和密码');
                        return;
                    }
                    axios({
                        url: 'http://hmajax.itheima.net/api/register',
                        method: 'post',
                        data: {
                            username: this.username,
                            password: this.password
                        }
                    }).then(res => {
                        console.log(res);
                    }).catch(err => {
                        console.log(err);
                    })
                    // console.log(this.username);
                    // console.log(this.password);
                }
            }
        }).mount('#app')
    </script>
</body>

</html>